package com.youth.framework.common.email;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * 发送邮件
 * 
 * @author pengsenlin
 * @version [版本号, 2014-2-2]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class HandleEmail {
	/**
	 * 发送邮件
	 * 
	 * @param mall
	 * @param mailTitle
	 * @param mailContent
	 * @return
	 * @throws Exception
	 */
	public boolean emailSend(String mall, String mailTitle, String mailContent)
			throws Exception {
		boolean boole = false;
		// 此处抽出来 几个地址备用
		String mailSmtpHost = "";
		String mailTransportProtocol = "";
		String mailUser = "";
		String mailPwd = "";
		String internetAddress = "";
		Properties props = new Properties();

		// 设置发送邮件的邮件服务器的属性
		props.setProperty("mail.transport.protocol", mailTransportProtocol);
		props.setProperty("mail.smtp.auth", "true");
		// 用刚刚设置好的props对象构建一个session
		Session session = Session.getDefaultInstance(props);
		// 有了这句便可以在发送邮件的过程中在console处显示过程信息，供调试使
		session.setDebug(false);
		Transport transport = null;
		// 用session为参数定义消息对象
		MimeMessage message = new MimeMessage(session);
		try {
			transport = session.getTransport();
			InternetAddress address[] = new InternetAddress[1];
			address[0] = new InternetAddress(mall);
			// 加载发件人地址
			message.setFrom(new InternetAddress(internetAddress));
			// 加载标题
			message.setSubject(mailTitle);
			// 设置邮件发送时间
			message.setSentDate(new Date());
			message.setContent(mailContent, "text/html; charset=utf-8");
			// 连接服务器的邮箱
			transport.connect(mailSmtpHost, mailUser, mailPwd);
			// 把邮件发送出去
			transport.sendMessage(message, new Address[] { new InternetAddress(
					mall) });
			transport.close();
			boole = true;
		} catch (MessagingException e) {
		}
		return boole;
	}

}
